feat(mypyc): proper weakref support#19056
feat(mypyc): proper weakref support#19056BobTheBuidler wants to merge 26 commits intopython:masterfrom
Conversation
|
I've added a I have not yet added any logic for setting supports_weakref, it is currently always set == cls.has_dict. I'm thinking to implement one or more of the options listed here: mypyc/mypyc#1102 Something in this PR has broken your serialization tests, it would be helpful if somebody could share some pointers wrt where I should look. |
|
Well... I tried fixing the serialization issue and now I just get segfaults instead. Not sure where to go from here. |
|
Great, the segfaults are fixed and the serialization tests are green Next steps:
Distant todo (maybe?):
|
|
Hell yeah, it works! |
|
TODO: I found this in cpython's docs: The only further addition is that tp_dealloc needs to clear any weak references (by calling PyObject_ClearWeakRefs()) if the field is non-NULL: https://docs.python.org/3.8/extending/newtypes.html#weak-reference-support |
|
I added some logic to the dealloc function as per the python docs above, it fails on all python versions for python 3.8-3.11 it fails due to the if check, and the fact that weakreflist is not a member of the struct. I believe this can either be fixed by adding the member to the struct (difficult, I noticed you have this commented in the code as a #TODO) or looking up the weakref field instead (potentially easier). I am blocked by not knowing how to access the weakref field here, or whether we can even access a python attribute in the dealloc function. for python 3.12 and 3.13 I think it segfaults because we need to #include weakref.h so we can call PyObject_ClearWeakRefs. I'm not sure of this however, and don't know how to emit the #include so I can test my theory. Leaving this PR as-is for now, will come back in a few days and work on it some more. |
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
* Fix weakref support in native classes * Update util.py
This is the beginning of a PR intended to fix a compatibility issue between mypyc and weakrefs
Fixes mypyc/mypyc#1102